/*!
\file MainWindow.cpp
\brief Implementation of MainWindow class.
\ingroup GUI
*/

#include"MainWindow.h" 

MainWindow::MainWindow(): QMainWindow(){
	m_visualizer = new Visualizer( this );
	setCentralWidget( m_visualizer );
	
	m_algorithmMenu = menuBar()->addMenu("Algorithm");
	
	m_graphMenu = menuBar()->addMenu("Graph");
	m_graphMenu->addAction( "Choose...", this, SLOT(graphTriggered()) );
	
	m_combination = new GraphAlgorithmCombination(this);
	m_combination->setAllowedAreas(Qt::LeftDockWidgetArea);
	addDockWidget(Qt::LeftDockWidgetArea, m_combination);
	connect( m_combination, SIGNAL(buttonClicked()), this, SLOT(combinationTriggered()) );
	
	m_lastSelectedGraph = 0;
}

MainWindow::~MainWindow(){
	
}

void MainWindow::addAlgorithm( AlgorithmInterface *a ){
	m_algorithmInterfaces.push_back( a );
	m_algorithmMenu->addAction( a->name(), this, SLOT(algorithmTriggered()) );
}

void MainWindow::addAlgorithm( Algorithm *a ){
	m_visualizer->addAlgorithm( a );
}

void MainWindow::algorithmTriggered(){
	m_combination->setAlgorithm( ((QAction*)sender())->text() );
}

void MainWindow::graphTriggered(){
	QString fileName = QFileDialog::getOpenFileName( this, "Choose a graph" );
	if( fileName.isEmpty() )
		return;
	
	Graph *temp = 0;
	if( fileName.endsWith(".xml") ){
		QString err;
		temp = XmlHandler::fromFile( fileName, err );
		if( temp == 0 && !err.isEmpty() ){
			QMessageBox::warning( this, "Error parsing graph", QString("ERROR: ").append(err) );
		}
	}else{
		temp = Graph::fromFile( fileName.toAscii().data() );
	}
	
	if( temp == 0 )
		return;
	
	DManager tempManager( temp->toDOT() );
	QPixmap graphPixmap = tempManager.toQPixmap();
	GraphConfirmationDialog dialog(graphPixmap, this);
	if( !dialog.exec() )
		return;
	m_combination->setGraph(graphPixmap.scaledToHeight(graphPixmap.height()/3));
	
	if( m_lastSelectedGraph != 0 )
		delete m_lastSelectedGraph;
	m_lastSelectedGraph = temp;
}

void MainWindow::combinationTriggered(){
	//TODO; This is not clean
	if( m_lastSelectedGraph == 0 ){
		QMessageBox::warning( this, "Error", "You have not selected a Graph yet." );
		return;
	}
	if( m_combination->algorithm().isEmpty() ){
		QMessageBox::warning(this, "Error", "You have not selected an Algorithm yet.");
		return;
	}
	
	Graph *g = m_lastSelectedGraph;
	m_lastSelectedGraph = 0;
	Algorithm *a = 0;
	QString name = m_combination->algorithm();
	for( int i = 0; i < m_algorithmInterfaces.size(); i++ ){
		if( m_algorithmInterfaces[i]->name() == name ){
			if( m_algorithmInterfaces[i]->canAcceptGraph(g) )
				a = m_algorithmInterfaces[i]->createAlgorithm(g);
			else{
				delete g;
				QMessageBox::critical(this, "Error", "This algorithm does not accept given graph.");
				return;
			}
		}
	}
	
	if( a==0 ){
		QMessageBox::critical(this, "Error", "Internal Error, could not get Algorithm.");
		return;
	}
	
	QMessageBox::information(this, "Information", "Chosen Algorithm will now run on given Graph.");
	m_visualizer->addAlgorithm(a, name);
	a->run();
	
	m_combination->setGraph(QPixmap());
	m_combination->setAlgorithm("");
}